<?php
header("Content-Type: text/html; charset=iso-8859-1");
require 'include.php';
$action = (!empty($_REQUEST['action'])) ? $_REQUEST['action'] : "";
if(check_admin("commission_technique")){
	$_SESSION['navigation'] = "equipes_admin";
	if(empty($_REQUEST['id'])){
		if($action == 'rank'){
			foreach($_POST as $key => $val){
				if($key == 'table_content'){
					foreach($val as $rank => $id){
						$q = "update equipes set num = '$rank' where id = '$id'";
						ExecRequete($q);
					}
				}
			}
		}
		else{
			if($action != 'list'){
				entete('equipes',"","",1);	
				$pied = 1;
			} 
			else $pied = 0;
			echo "<div id = 'action'>\n";
			if(!empty($_SESSION['equipe'])) unset($_SESSION['equipe']);
			echo "<h1>Les &eacute;quipes</h1>\n";
			$equipes = liste('equipes');
			$table = new table("equipes");
			$table->nb_rows = count($equipes);
			$table->filter = 1;
			$table->order = 'num';
			$table->styleclass = "";
			$table->sortable = "id";
			$fields = array("id","num","id_vbcll","nom","nom_complet");
			if(check_cookie() == 88) $fields[] = "emails";
			foreach($fields as $field){
				$$field = new table_field($field,"equipes");
				$$field->print = str_replace("_"," ",$field);
				if($field == 'num' || $field == 'id' || $field == 'emails') $$field->hidden = 1;
				$table->fields[] = $$field;
			}
			$action = new table_field('action',"equipes");
			$action->styleclass = 'center';
			$table->fields[] = $action;
			foreach($equipes as $id => $nom){
				$equipe = new equipe($id);
				$row = new table_row();
				$row->onclick = "style = \"cursor: move; \"";
				foreach($fields as $field){
					if($field == "emails") $row->cols[$field] = $equipe->get_emails();
					else $row->cols[$field] = $equipe->$field;
				}
				$key = key($table->rows) + 1;
				$row->cols['action'] = "<img src = 'images/icon_edit.png' style = 'cursor: pointer' alt = 'edit' width = '14' height = '14' onclick = 'edit_equipe(\"$id\")'/>";
				$row->cols['action'] .= "<img src = 'images/icon_delete.png' style = 'cursor: pointer' alt = 'sup.' width = '14' height = '14' onclick = 'delete_equipe(\"$id\",".$key.")'/>";
				$table->rows[] = $row; 
				
			}
			if(check_cookie() == 88){
				echo "<p class = 'small' style = 'text-align: right; font-size: 7pt; line-height: 7pt; padding-bottom: 0; margin-bottom: -15px;margin-right: 20px;' ><a href = 'javascript:show_emails()' id = 'show_emails'>afficher les emails</a></p>";
			}
			echo $table->print_header();
			echo $table->print_body();
			echo $table->print_footer();
			echo "<p class = 'center fade'>d&eacute;placer les lignes pour les ordonner</p>\n";
			echo "</div>\n";
			if($pied){
				echo "<script type = 'text/javascript'>\n";
		   		echo " Sortable.create('table_content',{tag:'tr',onUpdate:rank_equipes,});\n";  
				echo "</script>\n";
				pied();							
			} 
		}
	}
	else{
		$id = $_REQUEST['id'];
		$equipe = new equipe($id);
		if($action == 'get_edit'){
			entete('equipes',"","",1);
			echo "<div id = 'action'></div>\n";
			echo "<script type = 'text/javascript'>edit_equipe($equipe->id)</script>\n";
			pied();
		}
		elseif($action == 'calendrier'){
			$equipe->calendrier('',1);
		}
		elseif($action == 'nom_equipe'){
			$nom = (!empty($_REQUEST['nom'])) ? $_REQUEST['nom'] : $equipe->nom;
			$nom_complet = (!empty($_REQUEST['nom_complet'])) ? $_REQUEST['nom_complet'] : "";
			$q = "update equipes set nom = \"".(addslashes($nom))."\", nom_complet = \"".(addslashes($nom_complet))."\" where id = $equipe->id";
			if(ExecRequete($q)){
				echo $nom."<!--!>$nom_complet";
			}
			else echo $equipe->nom."<!--!>".$equipe->nom_complet;
		}
		elseif($action == 'edit'){
			$_SESSION['equipe'] = $equipe->id_vbcll;
			echo "<h1>$equipe->nom: $equipe->nom_complet <span class = 'float_right pointer' style = 'vertical-align: middle'><img src = 'images/icon_list.png' width = '18' height = '18' onclick = 'liste_equipes()' alt = 'list' style = 'vertical-align: top; padding-top:-3px; margin: 0;' /></span></h1>\n";
			echo "<h2>L'&eacute;quipe</h2>";
			echo "<form name = 'form_nom_equipe' id = 'form_nom_equipe' action = 'equipes_admin.php' method = 'post' >\n";
			$table = new table("nom_equipe");
			$fields = array("id" => "id","nom" => "ligue", "nom_complet" => "nom complet","action" => "action");
			foreach($fields as $field => $print){
				$$field = new table_field($field,"nom_equipe");
				$$field->print = $print;
				if($field == 'id') $$field->hidden = 1;
				elseif($field == 'action') $$field->styleclass = 'center';
				$table->fields[] = $$field;
			}
			$row = new table_row("nom_equipe");
			$row->cols['id'] = "10";
			$row->cols['nom'] = "<input type = 'hidden' name = 'id' value = '".$equipe->id."' /><input type = 'text' size = '5' name = 'nom' value = '$equipe->nom' />";
			$row->cols['nom_complet'] = "<input type = 'text' name = 'nom_complet' size = '40' value = '$equipe->nom_complet' />";
			$row->cols['action'] = "<button type = 'button' onclick = 'change_nom_equipe();'>modifier</button>";
			$row->hidden = 1;
			$table->rows[] = $row;
			$row = new table_row("nom_equipe");
			$row->cols['id'] = "1";
			$row->cols['nom'] = "<span id = 'form_nom'>$equipe->nom</span>";
			$row->cols['nom_complet'] = "<span id = 'form_nom_complet'>$equipe->nom_complet</span>";
			$row->cols['action'] = "<img class = 'pointer' src = 'images/icon_edit.png' alt = 'modif.' height = '14' width = '14' onclick = 'hide_row_table(1,1,\"id\",\"nom_equipe\");hide_row_table(10,0,\"id\",\"nom_equipe\");'>";
			$table->rows[] = $row;
			
			echo $table->print_header();
			echo $table->print_body();
			echo $table->print_footer();
			echo "</form>\n";
			echo "<h2>Les photos</h2>\n";
			$content = $equipe->photo();

			echo leftCol($content);
			$content =  "<div id='iframe'>\n";
			$content .=  "    <iframe src='photo_equipe_upload.php?id=$equipe->id' frameborder='' scrolling='0'></iframe>\n";
			$content .=  "</div>\n";

			$content .= "<p class = 'center'><button type = 'button' onclick =  \"return !window.open('crop_photo_equipe.php?equipe=$equipe->id','_blank','width=1000,height=1000,status=1,resizable=1,alwaysRaised=0,scrollbars=1');\">d&eacute;couper les photos de joueurs</button></p>\n";
			echo rightCol($content);
			echo "<br class = 'clear' />\n";
			
			echo "<form action  = 'equipes_admin.php' method = 'post' >\n";
			
			echo "<h2>Les entra&icirc;nements et matchs</h2>\n";
			echo "<div id = 'entrainements'>\n";
			echo "</div>\n";
			
			
			echo "<h2>Les responsables et entra&icirc;neurs</h2>\n";
			echo "<div id = 'responsables'></div>\n";
			
			
			echo "<h2>Les joueurs</h2>\n";
			echo "<div id = 'joueurs'></div>\n";
			echo "<h2>Le calendrier</h2>\n";
			echo "<div id = 'calendrier'>\n";
			$equipe->calendrier('',1);
			echo "</div>\n";
		}
		if($action == 'delete'){
			if(!empty($_REQUEST['confirm'])){
				$result = ($equipe->delete()) ? 1 : 0;
				if($result) echo "Equipe supprim&eacute;e";
				else echo "D&eacute;sol&eacute;, une erreur est survenue. L&#39;&eacute;quipe n&#39;a pas &eacute;t&eacute; effac&eacute;e correctement";
			}
			else{
				echo "<h3>Confirmer la suppression de $equipe->nom: $equipe->nom_complet</h3>\n";
				echo "<p class = 'center'><button type = 'button' onclick = 'confirm_delete_equipe($equipe->id)'><img src = 'icon_delete.png' alt = '' width = '14' height = '14' />supprimer</button>&nbsp;<button type = 'button' onclick = 'center_box(0);edit_equipe($equipe->id);'>annuler</button></p>\n";
			}
		}
	}
}
else redirect('perso.php?action=admin');
?>